package org.cs456;

import java.io.File;
import java.io.FileOutputStream;

import org.cs456.data.Image;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Environment;
import android.text.format.Time;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

public class TakePhotoActivity extends Activity implements LocationListener{
	private static final String TAG = "TakePhotoActivity ";
	Preview preview;
	Button buttonClick;
	Bitmap bmp;
	Time timestamp = new Time();
	Location loc;
	Location imgLoc;
	LocationManager locm;
	String locprovider = new String();

	
	
	private ShutterCallback shutterCallback = new ShutterCallback() {
		public void onShutter() {
			timestamp.setToNow();
			imgLoc = locm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
			Log.d(TAG, "onShutter'd");
		}
	};

	/** Handles data for jpeg picture */
	private PictureCallback jpegCallback = new PictureCallback() {
		public void onPictureTaken(byte[] data, Camera camera) {
			bmp = BitmapFactory.decodeByteArray(data, 0, data.length, null);
			showAltButtonSet();
			Log.d(TAG, "onPictureTaken - jpeg");
		}
	};

	/** Called when the activity is first created. */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.camera_layout);
		preview = new Preview(this);
		((FrameLayout) findViewById(R.id.preview)).addView(preview);
		
		locm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		Criteria c = new Criteria();
		c.setAccuracy(Criteria.ACCURACY_FINE);
		locm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 2.0f, this);
		
		Log.d(TAG, "onCreate'd");
	}
	
	@Override
	protected void onPause() {
		super.onDestroy();
		locm.removeUpdates(this);
	}
	
	@Override
	protected void onResume() {
		super.onDestroy();
		locm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 2.0f, this);
	}
	
	public void takePhoto(View view) {
		preview.camera.takePicture(shutterCallback, null, jpegCallback);
	}

	public void keepPhoto(View view) {
		try{

			File path =  Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES+ "/Phil Monroe");
			path.mkdirs();
			File file = new File(path, timestamp.format2445()+".jpg");
			Log.d("Phil Monroe", "Creating Directory: ");

			bmp.compress(CompressFormat.JPEG, 100, new FileOutputStream(file));
			Log.d("Phil Monroe", "File PAth: " + file.getAbsolutePath());

			Image img = new Image(getApplicationContext(), file.getAbsolutePath());
			img.saveToDatabase();

			img.setLocation(imgLoc);
			img.setTimestamp(timestamp);
			img.setMake("Motorola");
			img.setModel("Droid");
			
			img.setOrientation(getOrientation());
			
			img.EXIF.saveAttributes();
		}catch(Exception ex){
			Log.d("Phil Monroe", "POOP", ex);

		}
		
		

		bmp.recycle();
		bmp = null;
		Intent myIntent = new Intent(view.getContext(), MainActivity.class);
		startActivityForResult(myIntent, 0);
	}
	
	private int getOrientation(){
		return getResources().getConfiguration().orientation;
	}

	public void discardPhoto(View view) {
		showMainButtonSet();
		preview.camera.startPreview();
	}

	public void cancel(View view) {
		Intent myIntent = new Intent(view.getContext(), MainActivity.class);
		startActivityForResult(myIntent, 0);
	}

	// Private Methods
	// ------------------------------------------------------------
	private void showAltButtonSet() {
		Button keep = (Button) findViewById(R.id.keepPhotoButton);
		keep.setVisibility(View.VISIBLE);

		Button discard = (Button) findViewById(R.id.discardPhotoButton);
		discard.setVisibility(View.VISIBLE);

		Button take = (Button) findViewById(R.id.takePhotoButton);
		take.setVisibility(View.GONE);
	}

	private void showMainButtonSet() {
		Button keep = (Button) findViewById(R.id.keepPhotoButton);
		keep.setVisibility(View.GONE);

		Button discard = (Button) findViewById(R.id.discardPhotoButton);
		discard.setVisibility(View.GONE);

		Button take = (Button) findViewById(R.id.takePhotoButton);
		take.setVisibility(View.VISIBLE);
	}

	public void onLocationChanged(Location location) {
		loc = location;
		Log.v("Phil Monroe", "GPS Changed!");
	}

	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}

}
